layui.use(['LDLayout', 'layer'], function () {
    var LDLayout = layui.LDLayout,
        layer = layui.layer;

    // 设置中间内容区 最小高度
    LDLayout.setMinHeight();
    $(window).on('resize', LDLayout.setMinHeight);
    setViewVisible();
    /* 事件绑定 */
    $('.goods-edit')
        .delegate('.icon-reduce', 'click', function () {
            var $cartNum = $(this).siblings('.cart-num')
            var val = $cartNum.val();
            if (val - 1 >= $cartNum.data('limit')) {
                $(this).siblings('.cart-num').val(--val);
                setCheckedNum();
                updateSinglePrice(this, val);
            }
        })
        .delegate('.icon-plus', 'click', function () {
            var val = $(this).siblings('.cart-num').val();
            $(this).siblings('.cart-num').val(++val);
            setCheckedNum();
            updateSinglePrice(this, val);
        })
        .delegate('.cart-num', 'change', function () {
            if ($(this).val() < $(this).data('limit')) {
                $(this).val($(this).data('limit'))
            }
            setCheckedNum();
            updateSinglePrice(this, $(this).val());
        })

    // 全选 单选
    $('.checked-all-hook').on('change', function (ev) {
        var checked = this.checked;
        $('.checked-hook').each(function (i, item) {
            item.checked = checked;
        })
        $('.checked-all-hook').each(function (i, item) {
            item.checked = checked;
        })
        setCheckedNum();
    })

    $('.checked-hook').on('change', function (ev) {
        updateStatus();
    })

    // 删除商品
    $('.remove-hook').on('click', function () {
        var $parent = $(this).closest('.cart-item');
        openLayer('确定删除该商品?', function () {
            removeItems($parent);
            updateStatus();
        })
    })
    // 删除选中商品
    $('.remove-checked').on('click', function () {
        var $checkedHook = $('.checked-hook');
        var items = [];
        $checkedHook.each(function(i, item) {
            if (item.checked) {
                items.push($(item).closest('.cart-item'));
            }
        })
        if (items.length) {
            openLayer('确定选中商品?', function () {
                removeItems(items);
                updateStatus();
            })
        }
    })

    // 结算
    $('.checkout-hook').on('click', function () {
        var $checkedItem = $('.checked-hook');
        var checkCount = 0;
        $checkedItem.each(function (i, item) {
            if (item.checked) {
                checkCount += 1;
            }
        })
        if (checkCount > 0) {
            window.location.href = './order-confirm.html'
        } else {
            layer.msg('至少选中一个商品')
        }
    })
});
function setViewVisible () {
    var $cartItem = $('.cart-item');
    if ($cartItem.length) {
        $('.cart-view').show();
        $('.cart-empty').hide();
    } else {
        $('.cart-view').hide();
        $('.cart-empty').show();
    }
}
function updateStatus () {
    setCheckedAllStatus();
    setCheckedNum();
}
/* 设置购物车选中条目状态 */
function setCheckedAllStatus () {
    var $checkedItem = $('.checked-hook');
    var checkedCount = 0;
    $checkedItem.each(function (i, item) {
        if(item.checked) {
            checkedCount += 1;
        }
    });
    var checkedAll = checkedCount === $checkedItem.length && checkedCount !== 0;
    $('.checked-all-hook').each(function (i, item) {
        item.checked = checkedAll;
    });
}
/* 设置购物车底部数值 */
function setCheckedNum () {
    var $checkedItem = $('.checked-hook');
    var len = $checkedItem.length;
    var numCount = 0;
    var numType = 0;
    var priceCount = 0;
    $checkedItem.each(function (i, item) {
        if (item.checked) {
            var $parent = $(item).closest('.cart-item')
            var val = $parent.find('.cart-num').val();
            var price = $parent.find('.goods-price').data('price');
            numCount += Number(val);
            numType += 1;
            priceCount += Number(val) * Number(price);
        }
    })
    $('#checked_num_hook').text(numType);
    $('#checked_num_count_hook').text(numCount);
    $('#checked_count').text(setNumber(priceCount));
}
/* 更新单个商品价格 */
function updateSinglePrice (context, num) {
    var $parent = $(context).closest('.cart-item');
    var $count = $parent.find('.goods-count strong');
    var price = $parent.find('.goods-price').data('price');
    $count.text(setNumber(num * price));
}

/* 设置数值后补00 */
function setNumber (val) {
    var digital = Math.round(val * 100);
    var decimals = digital % 100;
    return digital / 100 + '' + (decimals ? '' : '.' + decimals + '0')
}

/* 弹出框 */
function openLayer (title, cb) {
    var layerIndex = layer.confirm(title, {title: '删除商品', skin: 'ld-layer'}, function(index){
        cb && cb();
        layer.close(layerIndex);
    });
}

/* 移除商品 */
function removeItems (items) {
    $.each(items, function(i, item) {
        $(item).remove();
    })
    setViewVisible();
}